﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BitPongTetrisAlgorithm;
using Microsoft.Win32;
using System.IO;

namespace BitPongTetrisUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.canvas.Background = Brushes.White;
            BitPongOutput.PlayGame(
@"-4096
28686
0
-4096
-1",
   this.canvas);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "Pic"; // Default file name
            sfd.DefaultExt = ".jpg"; // Default file extension
            sfd.Filter = "JPEG (.jpg)|*.jpg"; // Filter files by extension

            // Show save file dialog box
            bool? result = sfd.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                string path = sfd.FileName;
                Canvas workspace = this.canvas;

                FileStream fs = new FileStream(path, FileMode.Create);

                double sizeCoef = 2.5;
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)(workspace.ActualWidth * sizeCoef),
                    (int)(workspace.ActualHeight * sizeCoef), 200d, 200d, PixelFormats.Pbgra32);
                bmp.Render(workspace);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();   

                encoder.Frames.Add(BitmapFrame.Create(bmp));
                encoder.Save(fs);
                fs.Close();
            }
            
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "Pic"; // Default file name
            sfd.DefaultExt = ".tiff"; // Default file extension
            sfd.Filter = "TIFF (.tiff)|*.tiff"; // Filter files by extension

            // Show save file dialog box
            bool? result = sfd.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                string path = sfd.FileName;
                Canvas workspace = this.canvas;

                FileStream fs = new FileStream(path, FileMode.Create);

                double sizeCoef = 2.5;
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)(workspace.ActualWidth * sizeCoef),
                    (int)(workspace.ActualHeight * sizeCoef), 200d, 200d, PixelFormats.Pbgra32);
                bmp.Render(workspace);

                BitmapEncoder encoder = new TiffBitmapEncoder();

                encoder.Frames.Add(BitmapFrame.Create(bmp));
                encoder.Save(fs);
                fs.Close();
            }
        }
    }
}
